package com.augurit.gzcgwsp.csshlj.service.impl;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Random;

import org.hibernate.Query;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;

import com.augurit.common.util.HqlUtils;
import com.augurit.common.util.PageUtils;
import com.augurit.common.util.lang.DateUtils;
import com.augurit.common.util.lang.StringUtils;
import com.augurit.gzcgwsp.sendNotice.service.IYwMtbjlshInfoService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComQyxxService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjclService;
import com.augurit.gzcgwsp.ywcmn.service.IYwComSjcllistService;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComQyxxForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjclForm;
import com.augurit.gzcgwsp.ywcmn.web.form.YwComSjcllistForm;
import com.augurit.wf.app.doccode.service.IOaDocCodeService;
import com.augurit.wf.app.doccode.web.form.OaDocCodeForm;
import com.augurit.wf.common.service.impl.WfBusClientServiceImpl;


import com.augurit.gzcgwsp.csshlj.convert.CsshljczBaConvertor;
import com.augurit.gzcgwsp.csshlj.dao.CsshljczBaDao;
import com.augurit.gzcgwsp.csshlj.entity.CsshljczBa;
import com.augurit.gzcgwsp.csshlj.service.ICsshljczBaService;
import com.augurit.gzcgwsp.csshlj.form.CsshljczBaForm;
import com.augurit.gzcgwsp.csshlj.service.ICsshljCzService;
import com.augurit.gzcgwsp.csshlj.web.form.CsshljCzForm;

@Service
@Transactional
public class CsshljczBaServiceImpl extends WfBusClientServiceImpl<CsshljczBaForm> implements ICsshljczBaService{

	@Autowired
	private CsshljczBaDao csshljczBaDao;
    @Autowired
    private IOaDocCodeService doccodeService;
    @Autowired
    private IYwComQyxxService ywComQyxxService;
    @Autowired
    private IYwComSjclService sjclService;
    @Autowired
    private IYwComSjcllistService ywComSjcllistService;
    @Autowired
    private IYwComSjclService ywComSjclService;
	@Autowired
	private ICsshljCzService csshljCzService;
	@Autowired
    private IYwMtbjlshInfoService ywMtbjlshInfoService;
    
	/**
	 * ��������ȡForm����
	 */
	@Transactional(readOnly = true)
	public CsshljczBaForm get(Long id) {
		CsshljczBa entity = csshljczBaDao.get(id);
		CsshljCzForm czForm =	csshljCzService.get(entity.getCzid());
		CsshljczBaForm form=CsshljczBaConvertor.convertVoToForm(entity, czForm);
		return form;
        //return CsshljczBaConvertor.convertVoToForm(entity);
	}
	
	/**
	 * ɾ��Form�����б�
	 */
	public void delete(Long... ids) {
		csshljczBaDao.delete(ids);
	}

	/**
	 * �����������޸ĵ�Form�����б�
	 */
	public void save(CsshljczBaForm... forms) {
		if(forms != null)
			for(CsshljczBaForm form : forms)
				this.save(form);
	}
	
	/**
	 * �����������޸ĵ�Form����
	 */
	public void save(CsshljczBaForm form){
		if(form != null){
			CsshljczBa entity = null;
//            OaDocCodeForm doccodeForm = doccodeService.get(10L);
			//׼��VO����
			if(form != null && form.getId() != null){
				entity = csshljczBaDao.get(form.getId());
				if (StringUtils.isBlank(entity.getAcceptNo()) || form.getAcceptNo().indexOf("XXXX") > 0) form.setAcceptNo(null);
			}else{
				entity = new CsshljczBa();
                form.setAcceptNo(null);
			}
	        
	        boolean isNewForm = false;
            if (StringUtils.isBlank(form.getAcceptNo())) {
            	//生成政务中心统一查询编号 开始
    			Map<String,String> serialNoAndPasswordMap = null;
            	if("Y".equals(form.getSendNotice())){//网办，1
            		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("1");
            	}else{//大厅，0
            		serialNoAndPasswordMap = ywMtbjlshInfoService.getSerialNoAndPassword("0");
            	}
            	String serialNo = serialNoAndPasswordMap.get("serialNo");
            	String password = serialNoAndPasswordMap.get("password");
    			form.setSerialNo(serialNo);
    			form.setPassword(password);
    			form.setAcceptNo(serialNo);
        		//生成政务中心统一查询编号 结束
                isNewForm = true;            
            }
            
			CsshljczBaConvertor.convertFormToVo(form, entity);
			csshljczBaDao.save(entity);
			form.setId(entity.getId());
			if (isNewForm) {
                sjclService.updateByNewForm(form.getId());
            }
		}
	}

	/**
	 * ���Form����Ĳ�ѯ��������ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<CsshljczBaForm> search(Page<CsshljczBaForm> page, CsshljczBaForm form) {
		// �����µķ�ҳ����
		Page pg = PageUtils.newPageInstance(page);
		
		// ��ѯ��估����
		StringBuffer hql = new StringBuffer("from CsshljczBa ps where 1=1");
		List values = new ArrayList();
		
		// ��ѯ����
		if(form != null){
			
		}
		
		//����
		hql.append(HqlUtils.buildOrderBy(page, "ps"));
		
		// ִ�з�ҳ��ѯ����
		csshljczBaDao.findPage(pg, hql.toString(), values);
		
		// ת��ΪForm�����б?��ֵ��ԭ��ҳ������
		CsshljczBaConvertor convertor = new CsshljczBaConvertor();
		List<CsshljczBaForm> list = convertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	/**
	 * ��ݹ��������б�����ҳ��ѯ
	 */
	@Transactional(readOnly = true)
	public Page<CsshljczBaForm> search(Page<CsshljczBaForm> page, List<PropertyFilter> filters) {
		// �����µķ�ҳ����
		Page<CsshljczBa> pg = PageUtils.newPageInstance(page);
		
		// ������������ҳ���Ҷ���
		csshljczBaDao.findPage(pg, filters);
		
		CsshljczBaConvertor convertor = new CsshljczBaConvertor();
		List<CsshljczBaForm> list = convertor.convertVoListToFormList(pg.getResult());
		PageUtils.copy(pg, list, page);
		return page;
	}
	
	public void changeYslState(Long id, Long state) {
	    CsshljczBa entity = csshljczBaDao.get(id);
        //entity.setYsl(state);
        csshljczBaDao.save(entity);
    }
	
}
